﻿using System;
using UnityEngine;

// Token: 0x02000014 RID: 20
public class FirearmScript : MonoBehaviour
{
	// Token: 0x0600004F RID: 79 RVA: 0x00008A50 File Offset: 0x00006C50
	private void Start()
	{
		this.maxAmmo = this.ammo;
		this.rb = base.GetComponent<Rigidbody2D>();
		this.dmgObj = base.GetComponent<DamagingObject>();
		this.col = base.GetComponent<Collider2D>();
		this.rend = base.GetComponent<SpriteRenderer>();
	}

	// Token: 0x06000050 RID: 80 RVA: 0x00008A90 File Offset: 0x00006C90
	private void Update()
	{
		this.curCooldown -= Time.deltaTime;
		this.spriteCooldown -= Time.deltaTime;
		if (this.spriteCooldown < 0f && this.ammo > 0)
		{
			this.rend.sprite = this.fullSprite;
			return;
		}
		this.rend.sprite = this.emptySprite;
	}

	// Token: 0x06000051 RID: 81 RVA: 0x00008AFC File Offset: 0x00006CFC
	public void DropCasing()
	{
		Object.Destroy(Object.Instantiate(Resources.Load("BulletCasing"), base.transform.TransformPoint(new Vector2(0f, this.barrelPos.y)), base.transform.rotation), 360f);
	}

	// Token: 0x06000052 RID: 82 RVA: 0x00008B54 File Offset: 0x00006D54
	public static void FireBullet(Vector2 pos, Vector2 dir, float damage, float knockback, BodyScript ignoredBody, GameObject ignoredObj)
	{
		GameObject gameObject = Object.Instantiate(Resources.Load("MuzzleLine"), Vector2.zero, Quaternion.identity) as GameObject;
		gameObject.GetComponent<LineRenderer>().SetPosition(0, pos);
		gameObject.GetComponent<LineRenderer>().SetPosition(1, pos + dir * 500f);
		Object.Destroy(gameObject, 0.03f);
		RaycastHit2D[] array = Physics2D.RaycastAll(pos, dir, (float)LayerMask.GetMask(new string[]
		{
			"Grunt",
			"Default"
		}));
		int i = 0;
		while (i < array.Length)
		{
			RaycastHit2D raycastHit2D = array[i];
			if ((!ignoredBody || !ignoredBody.limbCols.Contains(raycastHit2D.collider)) && raycastHit2D.collider.gameObject != ignoredObj && !raycastHit2D.collider.isTrigger)
			{
				DamageReceiver damageReceiver;
				if (raycastHit2D.collider.TryGetComponent<DamageReceiver>(out damageReceiver))
				{
					if (damageReceiver.body.dodgeTime > 0f)
					{
						if (damageReceiver.body == GameManager.main.playerBody && GameManager.main.isArena)
						{
							PauseManager.main.TryUnlockAchievement(10);
							return;
						}
						break;
					}
					else
					{
						damageReceiver.DoWound(1, raycastHit2D.point);
						damageReceiver.body.DropBlood(damage, 1, raycastHit2D.point);
						if (damageReceiver.body == GameManager.main.playerBody)
						{
							damageReceiver.gameObject.SendMessage("Damage", damage * 0.5f, SendMessageOptions.DontRequireReceiver);
							GameManager.main.SpawnDamageNum(raycastHit2D.point, damage * damageReceiver.damageMult * 0.5f, damageReceiver.body);
						}
						else
						{
							damageReceiver.gameObject.SendMessage("Damage", damage, SendMessageOptions.DontRequireReceiver);
							GameManager.main.SpawnDamageNum(raycastHit2D.point, damage * damageReceiver.damageMult, damageReceiver.body);
						}
						AI ai;
						if (ignoredBody && damageReceiver.body.TryGetComponent<AI>(out ai))
						{
							ai.target = ignoredBody;
							if (ignoredBody.team != "goodguy")
							{
								ignoredBody.team = "rebel";
							}
						}
					}
				}
				if (raycastHit2D.rigidbody)
				{
					raycastHit2D.rigidbody.AddForceAtPosition(dir * knockback, raycastHit2D.point, ForceMode2D.Impulse);
				}
				else
				{
					((GameObject)Object.Instantiate(Resources.Load("BulletHole"), raycastHit2D.point + raycastHit2D.normal * Random.Range(-0.3f, 0.3f), Quaternion.Euler(0f, 0f, Vector2.SignedAngle(Vector2.up, raycastHit2D.normal)))).transform.SetParent(raycastHit2D.collider.transform);
				}
				gameObject.GetComponent<LineRenderer>().SetPosition(1, raycastHit2D.point);
				WeaponScript weaponScript;
				if (!raycastHit2D.collider.TryGetComponent<WeaponScript>(out weaponScript))
				{
					break;
				}
				weaponScript.durability -= damage * 1.5f;
				if (damage > 5f && !weaponScript.GetComponent<FirearmScript>())
				{
					FirearmScript.FireBullet(raycastHit2D.point, Vector2.Reflect(dir, raycastHit2D.normal), damage * 0.66f, knockback * 0.66f, null, raycastHit2D.collider.gameObject);
					Sound.Play(Resources.Load<AudioClip>("Sounds/rico" + Random.Range(1, 9).ToString()), raycastHit2D.point, true, true, 1f);
					return;
				}
				break;
			}
			else
			{
				i++;
			}
		}
	}

	// Token: 0x06000053 RID: 83 RVA: 0x00008F0C File Offset: 0x0000710C
	public void Fire()
	{
		if (this.curCooldown <= 0f && Time.timeScale > 0.1f)
		{
			this.curCooldown = this.fireCooldown;
			if (this.ammo > 0)
			{
				this.ammo--;
				this.DropCasing();
				this.rend.sprite = this.emptySprite;
				this.spriteCooldown = this.spriteFireShowTime;
				this.rb.AddForceAtPosition(Vector2.left * this.physRecoil * base.transform.localScale.x, base.transform.TransformPoint(this.barrelPos), ForceMode2D.Impulse);
				Sound.Play(this.fireSound, base.transform.position, false, false, 1f);
				GameObject gameObject = Object.Instantiate(Resources.Load("MuzzleFlash"), base.transform.TransformPoint(this.barrelPos), base.transform.rotation) as GameObject;
				gameObject.transform.localScale = base.transform.localScale;
				Object.Destroy(gameObject, 0.4f);
				FirearmScript.FireBullet(base.transform.TransformPoint(this.barrelPos), base.transform.right * base.transform.localScale.x, this.damage, this.knockback, this.dmgObj.currentOwner, base.gameObject);
				return;
			}
			this.dmgObj.currentOwner.ThrowWeapon();
			base.gameObject.AddComponent<DissapearHandler>();
			base.GetComponent<WeaponScript>().pickupable = false;
		}
	}

	// Token: 0x040000E6 RID: 230
	public float damage;

	// Token: 0x040000E7 RID: 231
	public float knockback;

	// Token: 0x040000E8 RID: 232
	public float rotRecoil;

	// Token: 0x040000E9 RID: 233
	public float physRecoil;

	// Token: 0x040000EA RID: 234
	public float fireCooldown;

	// Token: 0x040000EB RID: 235
	public float spriteFireShowTime;

	// Token: 0x040000EC RID: 236
	private float curCooldown;

	// Token: 0x040000ED RID: 237
	private float spriteCooldown;

	// Token: 0x040000EE RID: 238
	public int ammo;

	// Token: 0x040000EF RID: 239
	public AudioClip fireSound;

	// Token: 0x040000F0 RID: 240
	private Rigidbody2D rb;

	// Token: 0x040000F1 RID: 241
	public Vector2 barrelPos;

	// Token: 0x040000F2 RID: 242
	private DamagingObject dmgObj;

	// Token: 0x040000F3 RID: 243
	private Collider2D col;

	// Token: 0x040000F4 RID: 244
	public Sprite fullSprite;

	// Token: 0x040000F5 RID: 245
	public Sprite emptySprite;

	// Token: 0x040000F6 RID: 246
	private SpriteRenderer rend;

	// Token: 0x040000F7 RID: 247
	[HideInInspector]
	public int maxAmmo;
}
